<?php

require_once "LinkClass.inc";
// require_once "dpecho.inc";
require_once "helpers.inc";

function echoDpHeader($title)
{
	global $Context;
	global $marktime;

	$marktime = microtime(true);

	echo "
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html>
<head>
    <title>$title</title>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
    <link rel='stylesheet' type='text/css' href='./dp.css' />
</head>
<body>\n";

	echo "
    <div id='dpheader'>
        <div id='logo'>
        <img src='graphics/logo1.gif' 
            alt='Distributed Proofreaders' 
            height='60' />
    </div>
    <div class='subhdr'>
        Press On Regardless 
    </div>
    </div>\n";

	echo "
    <div class='quick'>
        <div style='float: right; height: 100%;'>\n";
            echoForumLinks();
    echo "
        </div>
        <div style='float: left; margin-top: -2px;'>\n";
    if( $Context->IsLoggedIn() ) {
            EchoTaskList();   
    }
    else {
        EchoLoginForm();
    }
        echo "
        </div>
    </div>\n";

    echo "<div class='spacer'>&nbsp;</div>\n";
}

function EchoLoginForm()
{
    // $url = self_url();
    echo "
            <form action='' method='post'>
              <label for='username'>User name</label> 
              <input type='text' class='dpinput' 
                     name='username' id='username'
                     size='10' maxlength='15' />

              <label for='password'>Password</label> 
              <input type='password' class='dpinput' 
                     name='password' id='password'
                     size='10' maxlength='15' />

              <input type='submit' style='margin-top: -2px' value='Login' />
            </form>\n";
}

function echoForumLinks()
{
	global $Context;
	global $User;
	global $page_name;

	$links = new Linklist();

/*
	$links->AddNamedLink("faq",     
                         "Help",                     
                         URL_FAQ,
                         "Frequently Asked Questions");

	if($Context->IsLoggedIn()) 
    {
		$links->AddNamedLink("inbox",   
                             "Home",             
                             ROOT_URL,
                             "Check your PMs.");
	} else {
*/
if(! $Context->IsLoggedIn()) {
		$links->AddNamedLink("register",   
                             "Register",             
                             REGISTER_URL,
                             "Register for Distributed Proofreaders.");
}

	$links->AddNamedLink("forum",   
                         "Forums",                   
                         FORUMS_URL,
                         "Review Forums.");

	if($Context->IsLoggedIn()) {
		$links->AddNamedLink("logout", 
                             "Log Out ({$User->Username()})", 
                             URL_LOGOUT);
	}

    $links->SetLinkStyle($Context->PageAlias(), 'active');

	$links->ListEcho();
}

function echoDpFooter()
{
	global $marktime;

    echo "<div class='spacer'>&nbsp;</div>\n";
	if(isset($marktime)) {
		$echotime = sprintf(" Build Time: %5.3f ", microtime(true) - $marktime);
	}
	else {
		$echotime = '';
	}
	echo "<div class='dpfooter'>
            Copyright Distributed Proofreaders of Canada
            &nbsp;&nbsp;&nbsp;$echotime
          </div>\n";

    echo "</body>\n</html>\n";
}

function EchoTaskList()
{
    global $DpDb;
    global $User;

    require_once "LinkClass.inc";
    $list = new LinkList();

    $sql = "SELECT
                t.taskCode,
                t.caption,
                t.instructionsurl
            FROM
                tasks AS t
            LEFT JOIN
                userquals AS uq
            ON
                t.taskcode = uq.qualcode
                AND t.tasktype = 'U'
                AND uq.username = '{$User->Username()}'
            WHERE
                NOT uq.username IS NULL
                OR t.tasktype = 'A'
            ORDER BY
                t.sequenceNumber";
    $rows = $DpDb->SqlObjects($sql);

    if( $User->IsMyProjects() ) {
        $list->AddNamedLink("myprojects",
                             "My Projects",        
                             URL_MY_PROJECTS,
                             "Projects you have worked on.");
    }

    foreach($rows as $row) {
        $url = build_path( ROOT_URL, $row->instructionsurl ) ;
        $list->AddNamedLink($row->taskCode, $row->caption, $url);
    }

    if($User->IsProjectManager()) {
        $list->AddNamedLink("PM", "PM", HREF_PROJECT_MGR) ;
    }

    $list->ListEcho();
}
?>
